<?php

namespace app\account\controller;

use app\BaseController;

use app\common\model\AccountModel;

use think\facade\Cache;

use GuzzleHttp\Client;

use ZipArchive;

class Account extends BaseController
{
    public function login($mobile,$pwd)
    {
        if(empty($mobile)){
            return error('请输入账号');
        }
        if(strlen($mobile) < 6 || is_phone($mobile)){
            return error('账号长度错误');
        }
        if(empty($pwd)){
            return error('请输入密码');
        }
        if(strlen($pwd) < 6){
            return error('密码长度错误');
        }
        if(null === $account_info = AccountModel::where('mobile',$mobile)->find()){
            return error('账号不存在');
        }
        if($account_info->pwd !== md5($pwd)){
            return error('密码错误');
        }
        if($account_info->status !== 1){
            return error('账号已被禁用');
        }
        $token = hash('md5', uniqid());
        Cache::set('account_token_'.$token,$account_info);
        return success(['token' => $token, 'role' => $account_info->role]);
    }
    public function detail()
    {
        return success([
            'account_id' => $this->request->account_info->account_id,
            'mobile' => $this->request->account_info->mobile,
            'name' => $this->request->account_info->name,
            'face' => $this->request->account_info->face,
            'role' => $this->request->account_info->role,
            'num' => $this->request->account_info->num,
        ]);
    }

    public function edit($pwd,$repwd){
        if($pwd !== $repwd){
            return error('密码不一致');
        }
        $account_info = $this->request->account_info;
        $account_info->pwd = $pwd;
        $account_info->save();
        // AccountModel::update(['pwd' => $pwd, 'account_id' => $account_info->account_id]);
        return success('密码修改成功');
    }
    
    public function version(){
        $path = root_path().'version.txt';
        if(!file_exists($path)){
            return error('版本号错误#101');
        }
        $version_data = file_get_contents($path);
        if(empty($version_data)){
            return error('版本号错误#102');
        }
        $data = [
            'type' => 'mp',
            'domain' => $_SERVER['SERVER_NAME'],
            'version_date' => $version_data
        ];
        $Client = new Client();
        $res = $Client->post('https://mp.xhm.net.cn/version/new',['form_params'=>$data])->getBody()->getContents();
        return json_decode($res,true);
    }
    
    public function upgrade(){
        return $this->decompression();
    }
    //下载升级包
    public function download(){
        $version_date = $this->request->param('version_date/s','');
        if(empty($version_date)){
            return error('升级失败#101');
        }
        $dir = root_path().'runtime/code/';
        if(!is_dir($dir)){
            mkdir($dir,0777,true);
        }
        $data = [
            'type' => 'mp',
            'domain' => $_SERVER['SERVER_NAME'],
            'version_date' => $version_date
        ];
        $file = $dir.'/'.$version_date.'.zip';
        $Client = new Client();
        $res = $Client->request('POST','https://mp.xhm.net.cn/version/upgrade',
            [
            'sink'=>$file,
            'form_params'=> $data
        ]);
        if($res->getStatusCode() == 200 && file_exists($file)) {
            return success('下载成功');
        }else{
            return error('下载失败');
        }
    }
    //解压包
    public function decompression(){
        $version_date = $this->request->param('version_date/s','');
        $dir = root_path().'runtime/code/';
        $file = $dir.'/'.$version_date.'.zip';
        if(!file_exists($file)){
            return error('文件不存在');
        }
        $zip=new ZipArchive;//新建一个ZipArchive的对象 
        if($zip->open($file)===TRUE){ 
            $zip->extractTo($dir); 
            $zip->close();
            unlink($file);
            return  success('解压成功');
        }
        return error('解压失败');
    }
    // 移动升级文件
    public function move(){
        $version_date = $this->request->param('version_date/s','');
        $dir = root_path().'runtime/code/'.$version_date;
        $phpDir = $dir.'/php/';
        $vueDir = $dir.'/vue/';
        
        if(is_dir($phpdir)){
            $res = doMoveDir($phpdir, root_path().'app/');
            if($res == false) return error ('移动PHP文件失败');
        }
        if(is_dir($vueDir)){
            $res = doMoveDir($vueDir, root_path().'public/');
            if($res == false) return error ('移动VUE文件失败');
        }
    
        return success('移动文件成功');
    }
    
    
    //执行数据库
    public function update(){
        $version_date = $this->request->param('version_date/s','');
        $dir = root_path().'runtime/code/'.$version_date;
        
        $sqlFile = $dir.'/update.sql';
        if(!file_exists($sqlFile)){
            return success('执行成功');
        }
       
        $sql = file_get_contents($sqlFile);
        $sqls = explode(';'."\r", $sql);
        if(empty($sql)) return success ('执行成功');
        Db::startTrans();
        try{
            foreach($sqls as $val){
               $val = trim($val);
               if(!empty($val)){
                  Db::execute($val);
               }
            }
            Db::commit();
            return success ('执行成功');
        } catch (\Exception $e){
            Db::rollback();
            return error($e->getMessage());
        }
        
    }
}
